package com.example.zhujiemian.txlb;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.zhujiemian.R;

import java.util.ArrayList;
import java.util.List;

public class FlowerAdapter extends RecyclerView.Adapter<FlowerAdapter.ViewHolder> {

    private List<Flower> flowerList;
    private Context context;
    private OnFlowerEditListener onFlowerEditListener;

    public interface OnFlowerEditListener {
        void onFlowerEdited(int position, Flower newFlower);
        void onFlowerDeleted(int position);
    }

    public FlowerAdapter(Context context, List<Flower> flowerList, OnFlowerEditListener listener) {
        this.context = context;
        this.flowerList = flowerList;
        this.onFlowerEditListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.flower_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        final Flower flower = flowerList.get(position);
        holder.flowerNameTextView.setText(flower.getName());
        holder.flowerAgeTextView.setText(String.valueOf(flower.getAge()));
        holder.flowerAddressTextView.setText(flower.getAddress());

        holder.editButton.setOnClickListener(v -> {
            // Show edit dialog
            showEditDialog(holder, position);
        });

        holder.deleteButton.setOnClickListener(v -> {
            // Delete the flower
            onFlowerEditListener.onFlowerDeleted(position);
            flowerList.remove(position);
            notifyItemRemoved(position);
        });
    }

    private void showEditDialog(final ViewHolder holder, final int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Edit Flower");

        // Set up the input
        final EditText nameEditText = new EditText(context);
        nameEditText.setText(flowerList.get(position).getName());
        builder.setView(nameEditText);

        // Set up the buttons
        builder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String newName = nameEditText.getText().toString();
                Flower editedFlower = new Flower(newName, flowerList.get(position).getAge(), flowerList.get(position).getAddress());
                onFlowerEditListener.onFlowerEdited(position, editedFlower);
                flowerList.set(position, editedFlower);
                notifyItemChanged(position);
            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        builder.show();
    }

    @Override
    public int getItemCount() {
        return flowerList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView flowerNameTextView;
        public TextView flowerAgeTextView;
        public TextView flowerAddressTextView;
        public View editButton;
        public View deleteButton;

        public ViewHolder(View itemView) {
            super(itemView);
            flowerNameTextView = itemView.findViewById(R.id.flower_name_text_view);
            flowerAgeTextView = itemView.findViewById(R.id.flower_age_text_view);
            flowerAddressTextView = itemView.findViewById(R.id.flower_address_text_view);
            editButton = itemView.findViewById(R.id.edit_button);
            deleteButton = itemView.findViewById(R.id.delete_button);
        }
    }
}
